package com.treasure.mall.biz.repository.user;

import com.treasure.mall.biz.base.JpaSearchableRepository;
import com.treasure.mall.biz.entity.embeddable.MerchantData;
import com.treasure.mall.biz.entity.user.MerchantPO;
import com.treasure.mall.zbq.common.ienum.live.LiveEnums;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 *
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.biz.repository.user
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/6/11 12:14
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 * Copyright 2019-2022 com.treasure.mall.biz.repository.user Inc. All rights reserved.
 */
@Repository
public interface MerchantRepository extends JpaSearchableRepository<MerchantPO, Long> {

    /**
     * 根据用户ID，获取商户数据
     *
     * @param id
     * @return
     */
    @Query("select merchant from MerchantPO merchant  where merchant.user.userId = ?1")
    MerchantPO findByUserId(Long id);

    MerchantPO findByUser_Mobile(String mobile);


    /**
     * 修改商家余额
     *
     * @param id
     * @param balance
     */
    @Query("update MerchantPO user set user.balance=user.balance+?2 where user.merchantId=?1")
    @Modifying
    @Transactional
    void modifyBalance(Long id, BigDecimal balance);

    /**
     * 修改历史总收入
     *
     * @param id
     * @param balance
     */
    @Query("update MerchantPO user set user.historyTotalAmount=user.historyTotalAmount+?2 where user.merchantId=?1")
    @Modifying
    @Transactional
    void modifyHistoryTotalAmount(Long id, BigDecimal balance);


    /**
     * 修改商家待入账余额
     *
     * @param userId
     * @param balance
     */
    @Query("update MerchantPO user set user.balanceInFuture=user.balanceInFuture+?2 where user.merchantId=?1")
    @Modifying
    @Transactional
    void modifyBalanceInFuture(Long userId, BigDecimal balance);

    @Query("update MerchantPO po set po.earnestMoney=po.earnestMoney+?2,po.version=po.version+1 " +
            "where po.version=?3 and po.merchantId=?1 and po.earnestMoney+?2>0")
    @Modifying
    @Transactional
    int modifyDeposit(Long userId, BigDecimal amount, long version);


    /**
     * 修改商家保证金
     *
     * @param id
     * @param money
     */
    @Query("update MerchantPO user set user.earnestMoney=?2 where user.merchantId=?1")
    @Modifying
    @Transactional
    void modifyEarnestMoney(Long id, BigDecimal money);


    /**
     * 审核店铺申请
     *
     * @param id
     * @param auditStatus
     * @param reason
     */
    @Query("update MerchantPO merchant set merchant.auditStatus=?2,merchant.reason=?3 where merchant.merchantId=?1")
    @Modifying
    @Transactional(rollbackFor = Exception.class)
    int modifyAuditStatus(Long id, LiveEnums.AuditStatus auditStatus, String reason);

    /**
     * 修改店铺数据
     *
     * @param id
     * @param data
     * @return
     */
    @Query("update MerchantPO merchant set merchant.data=?2 where merchant.merchantId=?1")
    @Modifying
    @Transactional(rollbackFor = Exception.class)
    int modifyData(Long id, MerchantData data);

    /**
     * 修改密码
     *
     * @param id
     * @param password
     * @return
     */
    @Query("update MerchantPO merchant set merchant.password=?2 where merchant.merchantId=?1")
    @Modifying
    @Transactional(rollbackFor = Exception.class)
    int modifyPassword(Long id, String password);


    /**
     * 查询当前类目下的商家数量
     *
     * @param categoryId
     * @return
     */
    @Query("select count(merchant) from MerchantPO  merchant where merchant.data.mainCategory.catId=?1")
    long countByCategory(Long categoryId);


    /**
     * 开启或关闭商户客服功能
     *
     * @param id
     * @param enableContact
     */
    @Query("update MerchantPO user set user.enableContact=?2 where user.merchantId=?1")
    @Modifying
    @Transactional
    void toggleContactStatus(Long id, boolean enableContact);


    long countByMerchantId(Long merchantId);
}
